<?php
/**
  * wechat php test  120.133.3.22
  */

//define your token
define("TOKEN", "wmjieyue");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{   //验证函数
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }
    //响应信息
    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                //定义msgType用于判断接收到的信息类型
                $msgType=$postObj->MsgType;
                $keyword = trim($postObj->Content);
                $RX_TYPE = trim($postObj->MsgType);
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>"; 
                $newsTpl="<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <ArticleCount>%s</ArticleCount>
                            %s
                        </xml>";
                // $eventTpl="<xml>
                //             <ToUserName><![CDATA[%s]]></ToUserName>
                //             <FromUserName><![CDATA[%s]]></FromUserName>
                //             <CreateTime>%s</CreateTime>
                //             <MsgType><![CDATA[%s]]></MsgType>
                //             <Event><![CDATA[%s]]></Event>
                //             <EventKey><![CDATA[%s]]></EventKey>
                //         </xml>";
                 // if(empty( $keyword ))
                 //    {
                 //        $msgType = "text";
                 //         $contentStr = "您好，欢迎来到借阅伴侣!\n新关注的用户如果不了解此借阅系统，可以点击底部菜单栏里面的借书规则，借/还书流程进行仔细阅读，了解后可进入图书馆开始您的阅读之旅。\n如果您在借阅过程中出现了不能解决的困难，可联系我们的客服：13166966052\n谢谢！";
                 //         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                 //         echo $resultStr;
                         
                 // }else{
                 //         echo "Input something...";
                        
                 // }
             
                if($msgType=='event'){
                    if($postObj->Event=='CLICK'){
                        if($postObj->EventKey=='V1002_WOOD'){
                            $msgType="news";
                            $count=1;
                            $str="<Articles>";
                            $str .="<item>
                                    <Title><![CDATA[借书流程]]></Title> 
                                    <Description><![CDATA[借阅伴侣借书流程如下：]]></Description>
                                    <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/2.jpg]]></PicUrl>
                                    <Url><![CDATA[http://mp.weixin.qq.com/s/JIcMJtgxmnra4HkwnPZclQ]]></Url>
                                    </item>";
                            $str .="</Articles>";
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$count,$str);
                            echo $resultStr;
                        }
                        if($postObj->EventKey=='V1004_HOOD'){
                            $msgType="news";
                            $count=1;
                            $str="<Articles>";
                            $str .="<item>
                                    <Title><![CDATA[联系我们]]></Title> 
                                    <Description><![CDATA[若您在使用过程中遇到任何问题，我们都欢迎咨询借阅伴侣客服部]]></Description>
                                    <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/u=2944579636,2120897455&fm=23&gp=0.jpg]]></PicUrl>
                                    <Url><![CDATA[http://mp.weixin.qq.com/s/AyEGov9beZ-Yz0mgUhI_LQ]]></Url>
                                    </item>";
                            $str .="</Articles>";
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$count,$str);
                            echo $resultStr;
                        }
                        if($postObj->EventKey=='V1001_GOOD'){
                            $msgType="news";
                            $count=1;
                            $str="<Articles>";
                            $str .="<item>
                                    <Title><![CDATA[借书说明]]></Title> 
                                    <Description><![CDATA[借阅伴侣借书说明如下：]]></Description>
                                    <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/u=3153582779,3907866477&fm=23&gp=0.jpg]]></PicUrl>
                                    <Url><![CDATA[http://mp.weixin.qq.com/s/EVRJagK-BSgxmAp9PlmyEQ]]></Url>
                                    </item>";
                            $str .="</Articles>";
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$count,$str);
                            echo $resultStr;
                        }
                        if($postObj->EventKey=='V1003_VOOD'){
                            $msgType="news";
                            $count=1;
                            $str="<Articles>";
                            $str .="<item>
                                    <Title><![CDATA[还书流程]]></Title> 
                                    <Description><![CDATA[借阅伴侣还书流程如下：‍]]></Description>
                                    <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/1.jpg]]></PicUrl>
                                    <Url><![CDATA[http://mp.weixin.qq.com/s/FDyOYwTMOCiCPlL6Z6zADQ]]></Url>
                                    </item>";
                            $str .="</Articles>";
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$count,$str);
                            echo $resultStr;
                        }
                    }
                }
                if($keyword=='2'){
                    $msgType="news";
                    $count=3;
                    $str="<Articles>";
                    // for($i=1;$i<=$count;$i++){
                    $str .="<item>
                            <Title><![CDATA[无微不至借阅伴侣]]></Title> 
                            <Description><![CDATA[借阅伴侣欢迎您]]></Description>
                            <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/112edef7d43432a25789187339612b49.jpg]]></PicUrl>
                            <Url><![CDATA[http://niceweekend.cc/jieyue]]></Url>
                            </item>
                            <item>
                            <Title><![CDATA[图书的发现]]></Title> 
                            <Description><![CDATA[借阅伴侣欢迎您]]></Description>
                            <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/qq_4f59b5272.png]]></PicUrl>
                            <Url><![CDATA[http://niceweekend.cc/jieyue/index.php/Home/List/liebiao.html]]></Url>
                            </item>
                            <item>
                            <Title><![CDATA[微信的了解]]></Title> 
                            <Description><![CDATA[借阅伴侣欢迎您]]></Description>
                            <PicUrl><![CDATA[http://niceweekend.cc/jieyue/Public/images/sina_467126d66.png]]></PicUrl>
                            <Url><![CDATA[]]></Url>
                            </item>";

                    // }
                    $str .="</Articles>";
                    $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$count,$str);
                    echo $resultStr;
                }
                           
				

        }else {
        	echo "";
        	exit;
        }
    }

	private function checkSignature()
	{
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        //接收参数
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>